import { createPinia } from 'pinia'
import piniaPluginPersist from 'pinia-plugin-persistedstate'
import useTagMenuStore from './tagMenuStore'
import useMainStore from './mainStore'
import { storeToRefs } from "pinia"

export const store = createPinia()

const createStore = (app) => {
    store.use(piniaPluginPersist)
    app.use(store)
    const modules = import.meta.glob("./*.js", { eager: true })
    for (const [file, module] of Object.entries(modules)) {
        const moduleName = file.replace(/^\.\/(.*?).js$/, '$1')
        storeInstance[moduleName] = module.default()
    }
}

const storeInstance = new Proxy({}, {
    get(instance, name) {
        return instance[name]
    }
})

export { createStore, useTagMenuStore, useMainStore, storeToRefs }

export default storeInstance